#!/usr/bin/env python2.5
# $Id: bootstrap.py 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# python imports
import os
import sys

__version__ = "$Revision: 7557b5d4d82b $"
__author__ = "$Author: pgurumur $"

def bootstrap(path):
    """
    Call this function as the first thing in your cron, or console script; it will bootstrap Django, and allow you to access all of the Django modules from the console, without using 'python manage.py shell'

    Examples:

        # A script within your django project.
        from django_bootstrap import bootstrap
        bootstrap(__file__)

    --- or ---

        # A cron script located outside of your django project
        from django_bootstrap import bootstrap
        bootstrap("/path/to/your/project/root/")
    """

    path = find_settings_path(path)
    parent_path = os.path.abspath(os.path.join(path, '..'))

    # Include path to settings.py directory
    os.sys.path.append(path)
    # Include path to django project directory
    os.sys.path.append(parent_path)

    from django.core import management
    try:
        import settings # Assumed to be in the os path
    except ImportError:
        sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
        sys.exit(1)

    management.setup_environ(settings)

def find_settings_path(path):
    """
    Retrieve the path of the settings.py module for a django project. This can be passed into django_utils.bootstrap). You can pass in the  __file__ variable, or an absolute path, and it will find the closest settings.py module.
    """
    if os.path.isfile(path):
        path = os.path.dirname(path)

    path = os.path.abspath(path)
    parent_path = os.path.abspath(os.path.join(path, '..'))
    settings_path = os.path.join(path, 'settings.py')

    if os.path.exists(settings_path):
        return path
    elif path != parent_path:
        return find_settings_path(parent_path)
    else:
        raise Exception('Could not find settings path.')

